package com.ruoyi.project.good.controller;

import java.util.List;

import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.framework.security.LoginUser;
import com.ruoyi.framework.security.service.TokenService;
import com.ruoyi.project.good.domain.Goods;
import com.ruoyi.project.good.service.IGoodHisService;
import com.ruoyi.project.good.service.IGoodsService;
import com.ruoyi.project.system.domain.SysUser;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.good.domain.GoodsType;
import com.ruoyi.project.good.service.IGoodsTypeService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 商品分类Controller
 * 
 * @author ruoyi
 * @date 2020-07-01
 */
@RestController
@RequestMapping("/good/type")
public class GoodsTypeController extends BaseController
{
    @Autowired
    private IGoodsTypeService goodsTypeService;

    @Autowired
    private TokenService tokenService;

    @Autowired
    private IGoodHisService goodHisService;

    @Autowired
    private IGoodsService goodsService;
    /**
     * 查询商品分类列表
     */
    @PreAuthorize("@ss.hasPermi('good:type:list')")
    @GetMapping("/list")
    public TableDataInfo list(GoodsType goodsType)
    {
        startPage();
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        SysUser user = loginUser.getUser();
        goodsType.setUserId(user.getUserId());

        List<GoodsType> list = goodsTypeService.selectGoodsTypeList(goodsType);
        return getDataTable(list);
    }

    /**
     * 导出商品分类列表
     */
    @PreAuthorize("@ss.hasPermi('good:type:export')")
    @Log(title = "商品分类", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(GoodsType goodsType)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        SysUser user = loginUser.getUser();
        goodsType.setUserId(user.getUserId());
        List<GoodsType> list = goodsTypeService.selectGoodsTypeList(goodsType);
        ExcelUtil<GoodsType> util = new ExcelUtil<GoodsType>(GoodsType.class);
        return util.exportExcel(list, "type");
    }

    /**
     * 获取商品分类详细信息
     */
    @PreAuthorize("@ss.hasPermi('good:type:list')")
    @GetMapping(value = "/getInfoById/{goodstypeId}")
    public AjaxResult getInfo(@PathVariable("goodstypeId") Long goodstypeId)
    {
        return AjaxResult.success(goodsTypeService.selectGoodsTypeById(goodstypeId));
    }

    /**
     * 新增商品分类
     */
    @PreAuthorize("@ss.hasPermi('good:type:add')")
    @Log(title = "商品分类", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody GoodsType goodsType)
    {

        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        SysUser user = loginUser.getUser();

        goodsType.setUserId(user.getUserId());
        goodsType.setCompanyId(user.getCompanyId());

        //判断本公司是否已存在相同名称的分类
        GoodsType t = goodsTypeService.selectGoodsTypeByName(goodsType);
        if(t != null && t.getIsDel() ==1){
            //存在已删除的类型，那就将类型状态改为可用状态
            goodsType.setIsDel(0l);
            return toAjax(goodsTypeService.updateGoodsType(goodsType));
        }else if(t != null && t.getName().equals(goodsType.getName())){
            //存在相同名字，并且正在使用的类型名称
            return new AjaxResult(HttpStatus.CONFLICT,"本公司该类型已存在");
        }

        if(goodsType.getParentId() != null){
            GoodsType g = goodsTypeService.selectGoodsTypeById(goodsType.getParentId());
            goodsType.setLevelCode(g.getLevelCode()+1);
        }else{
            goodsType.setLevelCode(1l);
        }
        goodHisService.addHis(user,"新增分类："+goodsType.getName(),"");
        return toAjax(goodsTypeService.insertGoodsType(goodsType));
    }

    /**
     * 修改商品分类
     */
    @PreAuthorize("@ss.hasPermi('good:type:edit')")
    @Log(title = "商品分类", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody GoodsType goodsType)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        SysUser user = loginUser.getUser();
        //判断本公司是否已存在相同名称的分类
        goodsType.setCompanyId(user.getCompanyId());
        GoodsType t = goodsTypeService.selectGoodsTypeByName(goodsType);
        if(t != null){
            if(t.getName().equals(goodsType.getName()) && t.getGoodstypeId() == goodsType.getGoodstypeId()){
                return new AjaxResult(HttpStatus.SUCCESS,"操作成功");
            }else if(t.getName().equals(goodsType.getName()) && t.getGoodstypeId() != goodsType.getGoodstypeId()){
                return new AjaxResult(HttpStatus.CONFLICT,"本公司该类型已存在");
            }
        }
        goodHisService.addHis(user,"修改分类："+goodsType.getName(),"");
        return toAjax(goodsTypeService.updateGoodsType(goodsType));
    }

    /**
     * 删除商品分类
     */
    @PreAuthorize("@ss.hasPermi('good:type:remove')")
    @Log(title = "商品分类", businessType = BusinessType.DELETE)
	@DeleteMapping("/dels/{goodstypeIds}")
    public AjaxResult remove(@PathVariable Long[] goodstypeIds)
    {
        return toAjax(goodsTypeService.deleteGoodsTypeByIds(goodstypeIds));
    }
    /**
     * 删除商品分类
     */
    @PreAuthorize("@ss.hasPermi('good:type:remove')")
    @Log(title = "商品分类", businessType = BusinessType.DELETE)
    @DeleteMapping("/del/{goodstypeId}")
    public AjaxResult remove(@PathVariable Long goodstypeId)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        SysUser user = loginUser.getUser();
        GoodsType goodstype = goodsTypeService.selectGoodsTypeByParentId(goodstypeId);
        if(goodstype != null){
            return AjaxResult.error("存在子分类，不允许删除");
        }
        List<Goods> list = goodsService.selectGoodsByTypeId(goodstypeId);
        if(list.size()>0){
            return AjaxResult.error("分类下有商品，请先删除商品");
        }
        goodHisService.addHis(user,"删除分类","id:"+goodstypeId);
        return toAjax(goodsTypeService.deleteGoodsTypeById(goodstypeId));
    }
}
